﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

/// <summary>
///CMSUser 的摘要说明
/// </summary>
public class CMSUser : Frame.BaseModel.FUser
{
    private CMSUser()
    {
    }

    string[] Rights;
    public static bool Login(string UserName, string Pwd)
    {
        using (DAL.DataClassesDataContext dataContext = new DAL.DataClassesDataContext())
        {
            DAL.css_user dUser = dataContext.css_user
                .FirstOrDefault(o => o.rybm == UserName && o.pwd == Pwd);
            if (dUser == null)
            {
                return false;
            }
            else
            {
                Current = new CMSUser()
                {
                    ID = dUser.rybm,
                    IsAdmin = (dUser.ryxm == "admin"),
                    m_isLogin = true,
                    Name = dUser.ryxm,
                    UserTypeName = dUser.bmbm,
                    Rights = dataContext.css_user_action.Where(u => u.rybm == dUser.rybm).Select(u => u.aid).ToArray()
                };
                return true;
            }
        }
    }

    public override bool CheckActionAllow(string ActionID)
    {
        if (!this.IsLogin)//当前是否登录用户
        {
            return false;
        }
        else
        {
            if (IsAdmin)//管理员账号，权限全开
            {
                return true;
            }
            else//其他人员的权限待补充
            {
                return Rights.Contains(ActionID);

            }
        }
    }

    /// <summary>
    /// 根据aid,rybm获得页面权限对象 
    /// </summary>
    /// <param name="aid"></param>
    /// <returns></returns>
    public static DAL.css_user_action GetFunction(string aid)
    {
        using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
        {
            return dc.css_user_action.FirstOrDefault(u => u.aid == aid && u.rybm == Current.ID);
        }
    }
}